package simulateOS.management.process;

import javafx.collections.ObservableList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import simulateOS.controller.ProcessPageController;
import simulateOS.entity.Process;
import simulateOS.entity.Status;
import simulateOS.management.memory.MemoryManager;

import java.util.HashMap;

@Slf4j
@Component
public class RR_executor implements Runnable{

    @Autowired
    private ProcessPageController processPageController;

    @Autowired
    private MemoryManager memoryManager;



    private int run_index = 0;

    private int last_run_index = 0;//用于辅助判断目标进程位置

    private int last_run_pcbId = -11111;

    private boolean isRun = true;

//    public static final HashMap<Integer, ProcessRunner> processRunnerReadyList = new HashMap<>();


    //RR算法的运行类
    private static RR_executor runner = null;

    //单例，确保线程独一无二
    public static RR_executor RRrunner(){
        if(runner == null){
            runner = new RR_executor();
            log.info("新建RR线程成功");
        }
        return runner;
    }

    private RR_executor() {

    }

    public static RR_executor getRR() {
        //获取本对象
        return runner;
    }

    public void runReadyList() {
        ObservableList<Process> readyList = processPageController.getReadyProcessList();
        int readyListSize = readyList.size();
        if(readyListSize==0) {
            return;
        }
        Process runProcess = readyList.get(run_index);

        int result = ((ProcessRunner) runProcess).runStr();
        run_index += result;
        try {
            Thread.sleep(420);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if(result==1) {
            processPageController.startRun2ReadyAnimation();
            runProcess.setStatus(Status.READY);
        }

        readyListSize = readyList.size();
        if(run_index>=readyListSize) {
            run_index = 0;
        }


//        //确定运行的下标，获取目标进程的Runner以及pcbId，以界面的队列为准
//        if(readyListSize==0) {
//            return;
//        }
//
//        Process runProcess;
//        int runPcbId;
//        if(last_run_pcbId==-11111) {
//            //第一个进程运行，记录第一个进程的pcbId;
//            runProcess = readyList.get(run_index);
//            runPcbId = runProcess.getPcb().getValue().getPid();
//            last_run_pcbId = runPcbId;
//        } else {
//            //之后的进程
//
//            for(int i=0;i<readyListSize;i++) {
//                if(readyList.get(i).getPcb().getValue().getPid()==last_run_pcbId) {
//                    last_run_index = i;
//                    if(last_run_index==(readyListSize-1)) {
//                        run_index = 0;
//                        break;
//                    } else {
//                        run_index = ++last_run_index;
//                        break;
//                    }
//                } else if(i==(readyListSize-1)) {
//                    //进程已不存在于列表中，启用下标查找
//                    run_index = last_run_index;
//                }
//            }
//
//
//            //xiabiao
//            runProcess = readyList.get(run_index);
//            runPcbId = runProcess.getPcb().getValue().getPid();
//            last_run_pcbId = runPcbId;
//
//        }
//
//        //运行选中的进程
//        ProcessRunner pr = processRunnerReadyList.get(runPcbId);
//        pr.runStr();

    }


    public void destroy() {
        isRun = false;
    }

    @Override
    public void run() {
        while(isRun) {
            //创建processRunner列表
            runReadyList();
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
        }
    }

    public int getRun_index() {
        return run_index;
    }

    public void setRun_index(int run_index) {
        this.run_index = run_index;
    }

}
